import { Bus, CONN_IMPORTANT } from "./Bus";
import { IBroadcast } from "./IBroadcast";

export class ImportantBroadcast extends IBroadcast {
    channelParam: any[] = [
        { event: "subscribe", group: "FLUCTUATION", topic: "ON_CHAIN" },
        { event: "subscribe", group: "BIG_AMOUNT", topic: "ON_CHAIN" }
    ];

    subscribe(): void {
        console.log("訂閱事件");
        this.unSubscribe();
        this.channelParam.forEach(i => {
            this.socket.send(JSON.stringify(i));
        });
    }

    unSubscribe(): void {
        this.channelParam.forEach(i => {
            // this.channelStr = JSON.stringify(i);
            const obj = { ...i };
            obj.event = "unsubscribe";
            // this.socket.send(JSON.stringify(obj));
            if (this.socket.readyState == 1) {
                this.socket.send(JSON.stringify(obj));
            } else {
                console.log("socket 已经关闭");
            }
        });
    }

    handle(jsondata: any): void {
        console.log("important", jsondata);
        Bus.emit(CONN_IMPORTANT, jsondata);
    }
}
